package com.colin.base.javaconcurrency.threadobjectclasscommonmethods;

/**
 * Created by Colin on 2020/3/4 15:13
 * email: colinzhaodong@gmail.com
 * desc: 3个线程，线程1和线程2首先被阻塞，线程3唤醒它们。notify, notifyAll。 start先执行不代表线程先启动
 *
 * @author zhaod
 */
public class WaitNotifyAll implements Runnable{
	private static final Object resourceA = new Object();

	public static void main(String[] args) throws InterruptedException {
		Runnable r = new WaitNotifyAll();
		Thread threadA = new Thread(r);
		Thread threadB = new Thread(r);
		Thread thread3 = new Thread(new Runnable() {
			@Override
			public void run() {
				synchronized (resourceA) {
//					resourceA.notifyAll();
                    resourceA.notify();
					System.out.println("ThreadC notified.");
				}
			}
		});
		threadA.start();
		threadB.start();
		Thread.sleep(200);
		thread3.start();
	}
	@Override
	public void run() {
		synchronized (resourceA){
			System.out.println(Thread.currentThread().getName()+" got resourceA lock.");
			try {
				System.out.println(Thread.currentThread().getName()+" waits to start.");
				resourceA.wait();
				System.out.println(Thread.currentThread().getName()+"'s waiting to end.");
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}
}
